package com.szosen.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedpreferenceUtil {
    /**
     * 保存用户的登录信息和用户的机构码信息等。
     */
    private static final String USERDATA = "userdata";
    private static final String OFFLINEUSERNAME = "offlineusername";
    private static final String OFFLINEPASSWORD = "offlinepassword";
    private static final String ONLINEUSERgovId = "onlinegovid";
    private static final String ONLINEDOCTORID = "onlinedoctorid";
    private static final String ONLINEUSERPASSWORD = "onlinepassword";
    private static final String HOSPITALNAME = "hospitalname";
    /**
     * serverAddress服务器的访问地址
     */
    private static final String SERVERADDRESS = "serveraddress";
    /**
     * 机构的guid
     */
    private static final String OFFLINEGUID = "offlineguid";
    /**
     * 用户的guid
     */
    private static final String OFFUSERLINEGUID = "offuserlineguid";
    private static SharedPreferences sharedPrefrences;
    private static Editor editor;
    private static final String TOKEN = "token";

    private static final String USE_WEB = "useweb";
    public static final String USE_WEB_NEIWANG = "neiwang";
    public static final String USE_WEB_WAIWANG = "waiwang";
    public static final String FIRSTTIME_USE = "firsttimeuse";
    public static final String YAOWUDATAS_GXY = "yaowudatas_gxy";
    public static final String YAOWUDATAS_TNB = "yaowudatas_tnb";
    public static final String YAOWUDATAS_SPLIT = "&";
    public static final String ID = "id";

    public static String getString(Context context, String key, String defValue) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        String result = sharedPrefrences.getString(key, defValue);
        return result;
    }

    public static boolean setString(Context context, String key, String value) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        editor.putString(key, value);
        editor.commit();
        return false;
    }

    public static boolean getboolean(Context context, String key) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        boolean result = sharedPrefrences.getBoolean(key, false);
        return result;
    }

    public static boolean setboolean(Context context, String key, boolean value) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * 设置使用的网路
     */
    public static void setUseWeb(Context context, String userName) {
        // BaseActivity.offlineSetGroup(new Group(count, passWord, guid));
        setString(context, USE_WEB, userName);
    }

    /**
     * 获取使用的网络类型。
     */
    public static String getUseWeb(Context context, String defValue) {
        return getString(context, USE_WEB, defValue);
    }

    public static void setNeedRememberPwd(Context context, boolean b) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        editor.putBoolean("needremember", b);
        editor.commit();
    }

    public static boolean getNeedRememberPwd(Context context) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        boolean result = sharedPrefrences.getBoolean("needremember", false);
        return result;
    }

    /**
     * 设置离线用户名
     */
    public static void setOffUserName(Context context, String userName) {
        // BaseActivity.offlineSetGroup(new Group(count, passWord, guid));
        setString(context, OFFLINEUSERNAME, userName);
    }

    /**
     * 获取离线的用户名
     */
    public static String getOffUserName(Context context, String defValue) {
        return getString(context, OFFLINEUSERNAME, defValue);
    }

    /**
     * 设置离线的密码
     */
    public static void setOffPassword(Context context, String id) {
        setString(context, OFFLINEPASSWORD, id);
    }

    /**
     * 获取离线的密码
     */
    public static String getOffPassword(Context context, String defValue) {
        return getString(context, OFFLINEPASSWORD, defValue);
    }

    /**
     * 获取密码
     */
    public static String getOnPassword(Context context, String defValue) {
        return getString(context, ONLINEUSERPASSWORD, defValue);
    }

    /**
     * 设置密码
     */
    public static void setOnPassword(Context context, String id) {
        setString(context, ONLINEUSERPASSWORD, id);

    }

    /**
     * 设置离线的guid
     */
    public static void setOffGUID(Context context, String guid) {
        setString(context, OFFLINEGUID, guid);
    }

    /**
     * 获取离线的guid
     */
    public static String getOffGUID(Context context, String defValue) {
        return getString(context, OFFLINEGUID, defValue);
    }

    /**
     * 设置离线的用户guid
     */
    public static void setUserOffGUID(Context context, String guid) {
        setString(context, OFFUSERLINEGUID, guid);
    }

    /**
     * 获取离线的用户guid
     */
    public static String getUserOffGUID(Context context, String defValue) {
        return getString(context, OFFUSERLINEGUID, defValue);
    }

    /**
     * 设置在线用户名
     */
    public static void setOnGovId(Context context, String userName) {
        // BaseActivity.offlineSetGroup(new Group(count, passWord, guid));
        setString(context, ONLINEUSERgovId, userName);
    }

    /**
     * 获取在线的用户名
     */
    public static String getOnGovId(Context context, String defValue) {
        return getString(context, ONLINEUSERgovId, defValue);
    }

    /**
     * 设置在线的密码
     */
    public static void setOnDoctorId(Context context, String password) {
        setString(context, ONLINEDOCTORID, password);
    }

    /**
     * 获取在线的密码
     */
    public static String getOnDoctorId(Context context, String defValue) {
        return getString(context, ONLINEDOCTORID, defValue);
    }

    /**
     * 设置医院名称
     */
    public static void setHospitalName(Context context, String hospitalName) {
        setString(context, HOSPITALNAME, hospitalName);
    }

    /**
     * 获取医院名称
     */
    public static String getHospitalName(Context context, String hospitalName) {
        return getString(context, HOSPITALNAME, hospitalName);
    }

    /**
     * 设置服务器访问地址
     */
    public static void setserverAddress(Context context, String serverAddress) {
        setString(context, SERVERADDRESS, serverAddress);
    }

    /**
     * 获取服务器访问地址
     */
    public static String getserverAddress(Context context, String serverAddress) {
        return getString(context, SERVERADDRESS, serverAddress);
    }

    /**
     * 设置第一次使用标记
     */
    public static void setFirstTimeUse(Context context, boolean value) {
        setboolean(context, FIRSTTIME_USE, value);
    }

    /**
     * 获取医院名称
     */
    public static boolean getFirstTimeUse(Context context) {
        return getboolean(context, FIRSTTIME_USE);
    }

    /**
     * 设置医院名称
     */
    public static void setYaowuDatas(Context context, String yaowudatas,
                                     String type) {
        switch (type) {
            case YAOWUDATAS_GXY:
                setString(context, YAOWUDATAS_GXY, yaowudatas);
                break;
            case YAOWUDATAS_TNB:
                setString(context, YAOWUDATAS_TNB, yaowudatas);
                break;

            default:
                break;
        }
    }

    /**
     * 获取药物数据
     */
    public static String getYaowuDatas(Context context, String defaultStr,
                                       String type) {
        switch (type) {
            case YAOWUDATAS_GXY:
                return getString(context, YAOWUDATAS_GXY, defaultStr);
            case YAOWUDATAS_TNB:
                return getString(context, YAOWUDATAS_TNB, defaultStr);
        }
        return "";
    }

    /**
     * 设置第一次使用标记
     */
    public static void setToken(Context context, String token) {
        setString(context, TOKEN, token);
    }

    /**
     * 获取医院名称
     */
    public static String getToken(Context context, String defalueStr) {
        return getString(context, TOKEN, defalueStr);
    }

    public static void setUseSannuo(Context context, boolean b) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        editor.putBoolean("usesannuo", b);
        editor.commit();
    }

    public static boolean getUseSannuo(Context context) {
        if (sharedPrefrences == null) {
            sharedPrefrences = context.getSharedPreferences(USERDATA,
                    Context.MODE_PRIVATE);
            editor = sharedPrefrences.edit();
        }
        boolean result = sharedPrefrences.getBoolean("usesannuo", true);
        return result;
    }
}
